package com.kyrioscraft.cloud.security.apprest;

import com.kyrioscraft.cloud.security.core.properties.SecurityProperties;
import com.kyrioscraft.cloud.security.core.social.AppSignUpUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author kyrioscraft
 * @Date 2018/12/29 14:10
 * @Description //TODO
 **/
@RestController
@Slf4j
public class UserController {

    @Autowired
    private AppSignUpUtils appSignUpUtils;
    @Autowired
    private SecurityProperties securityProperties;

    @PostMapping("/regist")
    public void regist(HttpServletRequest request){

        //用户注册生成的userId
        String userId = "123";
        appSignUpUtils.doPostSignUp(new ServletWebRequest((request)),userId);
    }

    @GetMapping("/me")
    public Object getCurrentUser(Authentication authentication, HttpServletRequest request) {
        String token = StringUtils.substringAfter(request.getHeader("Authorization"),"bearer");
//        Claims claims = Jwts.parser().setSigningKey(securityProperties.getOauth2().getJwtSigningKey().getBytes()).parseClaimsJwt(token).getBody();

        Jwt decode = JwtHelper.decode(token);
        String claims = decode.getClaims();
//        String loginType = (String) claims.get("loginType");
        log.info(claims);
        return authentication;
    }
}
